//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;
using OpenLS.Vml.Drawing;

namespace OpenLS.Vml.Serialization
{
    public class VmlInstanceProvider: IOfficeInstanceProvider
    {

        public IOfficeXmlSerializable ReadInstance(ReadContext context)
        {
            switch (context.Reader.NamespaceURI)
            {
                case VmlConstants.VmlNamespaceUri:
                    {
                        return Read (createVmlInstance(context), context);
                    }
                case VmlConstants.OfficeNamespaceUri:
                    return Read (createOfficeInstance(context), context);
                case VmlConstants.ExcelNamespaceUri:
                case VmlConstants.WordNamespaceUri:
                    throw new NotImplementedException(context.Reader.NamespaceURI);
                default:
                    return null;

            }
        }
        IOfficeXmlSerializable Read (IOfficeXmlSerializable s, ReadContext c)
        {
            if (s != null)
                s.ReadXml(c);
            return s;
        }
        private static IOfficeXmlSerializable createOfficeInstance(ReadContext context)
        {
            switch (context.ReaderLocalName)
            {
                case "shapedefaults":
                    return new ShapeDefaults();
                case "shapelayout":
                    return new ShapeLayout();
                case "OLEObject":
                    return new OleObject();
                default:
                    throw new NotImplementedException(context.ReaderLocalName);
            }
        }

        private static IOfficeXmlSerializable createVmlInstance(ReadContext context)
        {
            switch (context.ReaderLocalName)
            {
                case Group.xmlName:
                    return new Group();
                case "shapetype":
                    return new ShapeType();
                case "shape":
                    return new Shape();
                case "rect":
                    return new Rect();
                case "line":
                    return new Line();
                case "roundrect":
                    return new RoundRect();
                case "oval":
                    return new Oval();

                default:
                    throw new NotImplementedException(context.ReaderLocalName);
            }
        }
    }
}